package net.borlis.sample.web.weld;

import net.borlis.configuration.context.apache.ConfigurationData;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.PersistenceException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author alex@borlis.net
 */
@Deprecated
public final class ContextConfigInitiator {

    private static final Logger LOGGER = Logger.getLogger(ContextConfigInitiator.class.getName());

    private static final String CONFIGURATION_RESOURCE = "java:/comp/env/dpct/configuration";

    private ContextConfigInitiator() {
    }

    public static ConfigurationData getContextData() {
        try {

            LOGGER.info("INITIALIZING WS_IMPL getContext() ...");
            InitialContext ic = new InitialContext();
            LOGGER.info("INITIALIZED WS_IMPL getContext(), LOOKUP FOR RESOURCE ...");
            ConfigurationData configurationData = (ConfigurationData) ic.lookup(CONFIGURATION_RESOURCE);
            LOGGER.info("LOOKUP FOUND CONFIGURATION: " + configurationData.toString());

            LOGGER.info("context configuration schema = " + configurationData.getSchemaVersion());
            LOGGER.info("context configuration server = " + configurationData.getDatabaseServer());
            LOGGER.info("context configuration port = " + configurationData.getDatabasePort());
            LOGGER.info("context configuration user = " + configurationData.getDatabaseUser());
            LOGGER.info("context auto update = " + configurationData.getDatabaseAutoUpdate());

            return configurationData;
        } catch (PersistenceException | NamingException e) {
            LOGGER.log(Level.SEVERE, "Could not create environment", e);
            throw new IllegalStateException(e);
        }

    }

}